<?php
namespace App\Models\AppModels;
use App\Models\ParentModel;
use DB;
use App\Components\Tools;
/**
 * 移动端api请求数据处理model
 * @author puppet
 */
class Api extends ParentModel{
	/**
	* 与模型关联的数据表。
	*
	* @var string
	*/
	protected $table = 'pt_dy_access_token';

	/**
	* DB::直接操作的数据库表名
	* @var string
	*/
	protected $db_table = 'pt_dy_access_token';

	/**
	* 主键 默认id。
	*
	* @var string
	*/
	protected $primaryKey = 'id';

	/**
	* 指定是否模型应该被戳记时间。
	*
	* @var bool
	*/
	public $timestamps = false;

    /**
     * 翻页展示关键词数据（内容关键词）type 0内容关键词   否则 标题关键词
     */
    function getKeyData($data){
        $user_id=isset($data['uid']) ? $data['uid'] : 0;
        if($data['type']){
            $sql="select * from pt_keyword where user_id = ? and status!=-1 ";
            if (isset($data['name']) && !empty($data['name'])) {
                $sql .= " AND name like '%{$data['name']}%'";
            }
            $sql.=" order by status desc,update_time desc";
            $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re : false;
        }else{
            $sql="select * from pt_keyword_conent where user_id = ? and status!=-1";
            if (isset($data['name']) && !empty($data['name'])) {
                $sql .= " AND name  like '%{$data['name']}%'";
            }
            $sql.=" order by status desc,update_time desc";
               $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re : false;
        }
        return $re;
    }
    /**
     * 翻页展示所有信息
     *	询盘列表展示
     */
    public function getPageData($params){
        $id=$params['id'];
      //   print_r($params);exit();
        $res = false;
        $getDate=date('Y-m-d H:i:s');
        $dd=date('Y-m-d H:i:s',strtotime('-1day'));
//        print_r($getDate);print_r($dd);exit();
        $limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
        $limit_sta = 0;
        $page = 1;
        if (isset($params['page']) && intval($params['page']) > 0) {
            $limit_sta = intval($limit_num * intval($params['page'] - 1));
            $page = intval($params['page']);
        }
        $cou_sql = "SELECT count(*) as count FROM pt_partner_comment WHERE partner_id = ?";
        $sql = "SELECT a.id,a.create_time,a.status,b.name,c.nickname,c.userImg,c.content,c.user_id,c.video_url,c.pt_type,c.uid,c.title,c.keyword,c.tells,c.video_id,c.sec_uid FROM pt_partner_comment as a ";
        $sql.="LEFT JOIN pt_keyword as b on b.id=a.keyword_id LEFT JOIN pt_keyword_video_comment as c on c.id=a.comment_id WHERE a.partner_id=?";
        $values = [$id];
        if (isset($params['start_time']) && !empty($params['start_time'])) {
            $start_time = $params['start_time'] . ' 00:00:00';
            $cou_sql .= " AND create_time >= ?";
            $sql .= " AND a.create_time >= ?";
            $values[] = $start_time;
        }
        if (isset($params['end_time']) && !empty($params['end_time'])) {
            $end_time = $params['end_time'] . ' 23:59:59';
            $cou_sql .= " AND create_time <= ?";
            $sql .= " AND a.create_time <= ?";
            $values[] = $end_time;
        }
        if (isset($params['aday']) && !empty($params['aday'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
            //  print_r($getDate);print_r($oldDate);exit();
            $cou_sql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if (isset($params['week']) && !empty($params['week'])) {
            $getDate=date('Y-m-d H:i:s',strtotime('+0day'));
            $oldDate=date('Y-m-d ',strtotime('-1week'));
            $cou_sql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if(isset($params['status']) && $params['status']!='99'){
            $cou_sql.=" and status = {$params['status']}";
            $sql.=" and a.status = {$params['status']}";
        }
        if (isset($params['month']) && !empty($params['month'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1month'));
            $cou_sql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if (isset($params['month']) && !empty($params['month'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1month'));
            $cou_sql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if (isset($params['month']) && !empty($params['month'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1month'));
            $cou_sql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        $count = DB::select($cou_sql, $values);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        //print_r($sql);exit();
        $sql .= "  order by a.id desc LIMIT $limit_sta,$limit_num";
        $data_list = DB::select($sql, $values);
        $date['data_list']=$data_list;
        $date['total']=$count;
        return $date;
    }
    /**
     *	获取用户监控页面数据
     *	$type 1(主页用户)  2(单视频用户) 3(广告用户)   $page 第几页   $role_id角色id   partner_id登录的ID
     */
    function selectData($parames){
        $partner_id=$parames['id'];
        $type=isset($parames['type']) ? intval($parames['type']) : 0;

        $size = (isset($parames['limit']) && intval($parames['limit'])>0 && intval($parames['limit']) <= 100) ? $parames['limit'] : 10;
        $page = isset($parames['page']) ? $parames['page'] : 1;
        $offset = $size * ($page - 1);

        $countSql="select count(id) as count from pt_user_info where info_type = ? and partner_id = {$partner_id} and system_type = 0";
        $sql="select id,system_type,partner_id,info_id,info_name,info_img,info_signature,info_follow,info_fans,info_support,info_opus,info_type,info_vurl,info_date,number_id,status from pt_user_info where info_type = ? and partner_id = {$partner_id} and system_type = 0";
        if (isset($params['start_time']) && !empty($params['start_time'])) {
            $start_time = $params['start_time'] . ' 00:00:00';
            $countSql .= " AND create_time >= ?";
            $sql .= " AND a.create_time >= ?";
            $values[] = $start_time;
        }
        if (isset($parames['end_time']) && !empty($parames['end_time'])) {
            $end_time = $parames['end_time'] . ' 23:59:59';
            $countSql .= " AND create_time <= ?";
            $sql .= " AND a.create_time <= ?";
            $values[] = $end_time;
        }
        if (isset($parames['aday']) && !empty($parames['aday'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
            //  print_r($getDate);print_r($oldDate);exit();
            $countSql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if (isset($parames['week']) && !empty($parames['week'])) {
            $getDate=date('Y-m-d H:i:s',strtotime('+0day'));
            $oldDate=date('Y-m-d ',strtotime('-1week'));
            $countSql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if(isset($parames['status']) && $parames['status']!='99'){
            $countSql.=" and status = {$parames['status']}";
            $sql.=" and a.status = {$parames['status']}";
        }
        if (isset($parames['month']) && !empty($parames['month'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1month'));
            $countSql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if (isset($params['month']) && !empty($params['month'])) {
            $getDate=date('Y-m-d H:i:s');
            $oldDate=date('Y-m-d H:i:s',strtotime('-1month'));
            $countSql .= " AND create_time  between '$oldDate' and '$getDate'";
            $sql .= " AND a.create_time  between '$oldDate' and '$getDate'";
        }
        if(isset($parames['nickname']) && !empty($parames['nickname']) ){
            $countSql.=" and info_name like '%{$parames['nickname']}%'";
            $sql.=" and info_name like '%{$parames['nickname']}%'";
        }

        if($type==1){
            $sql.=" order by status desc,info_date desc limit $offset,$size";
        }else{
            $sql.=" order by info_date desc limit $offset,$size";
        }

        // echo $countSql;exit;
        $count=DB::select($countSql,[$type]);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        $data=DB::select($sql,[$type]);
        $date['data_list']=$data;
        $date['total']=$count;
        return $date;
    }
    //获取询盘数据拉取数据20条	type	0视频，否则主页
    function getVideoViewComment($parames){
        $user_id=$parames['id'];
        $size = 20;
        $hour = isset($parames['hour']) ? intval($parames['hour']) : 0;
        $day = isset($parames['day']) ? intval($parames['day']) : 0;
        $week = isset($parames['week']) ? intval($parames['week']) : 0;
        $offset = $size * ($parames['page'] - 1);
        if(!$parames['type']){
            $countsql="select count(id) as count from pt_user_info_comment where type=0 and partner_id={$user_id} and video_id=?";
            $sql="select * from pt_user_info_comment where type=0 and partner_id={$user_id} and video_id=? ";
            if($hour==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1 hours' ));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($day==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($week==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1week'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }
            $count=DB::select($countsql,[$parames['video_id']]);
            $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
            $sql.=" order by create_time desc  limit {$offset},{$size}";
            $re=DB::select($sql,[$parames['video_id']]);
            $re=isset($re[0]['id']) ? $re : false;
        }else{
            $countsql="select count(id) as count from pt_user_info_comment where type={$parames['type']} and partner_id=?";
            $sql="select * from pt_user_info_comment where type={$parames['type']} and partner_id=?";
            if($hour==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1 hours' ));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($day==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1day'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }elseif ($week==1){
                $getDate=date('Y-m-d H:i:s');
                $oldDate=date('Y-m-d H:i:s',strtotime('-1week'));
                $countsql.= " AND create_time  between '$oldDate' and '$getDate'";
                $sql.=  " AND create_time  between '$oldDate' and '$getDate'";
            }
            $sql.=" order by create_time desc limit {$offset},{$size}";
            $count=DB::select($countsql,[$user_id]);
            $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
            $re=DB::select($sql,[$user_id]);
            $re=isset($re[0]['id']) ? $re : false;
        }

         $nextPage=$parames['page'] < ceil($count/$size) ? $parames['page']+1 : 0;
        $date['data_list']=$re;
        $date['total']=$count;
        $date['nextPage']=$nextPage;
        return $date;
    }
    //获取直播列表数据
	public function getLiveData($parames){
            $partner_id=$parames['id'];
            $type=isset($parames['type']) ? intval($parames['type']) : 0;

            $size = (isset($parames['limit']) && intval($parames['limit'])>0 && intval($parames['limit']) <= 100) ? $parames['limit'] : 10;
            $page = isset($parames['page']) ? $parames['page'] : 1;
            $offset = $size * ($page - 1);

            $countSql="select count(id) as count from pt_user_live where  partner_id = {$partner_id} ";
            $sql="select id,system_type,partner_id,room_id,info_name,info_type,create_time,update_time,status,room_url from pt_user_live where  partner_id = {$partner_id} ";

            if(isset($parames['info_name']) && !empty($parames['info_name']) ){
                $countSql.=" and info_name like '%{$parames['nickname']}%'";
                $sql.=" and info_name like '%{$parames['nickname']}%'";
            }
            if($type==1){
                $sql.=" order by id desc limit $offset,$size";
            }else{
                $sql.=" order by create_time desc limit $offset,$size";
            }
            // echo $countSql;exit;
            $count=DB::select($countSql);
            $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
            $data=DB::select($sql);
            $date['data_list']=$data;
            $date['total']=$count;
            return $date;
        }

}